//
//  AddCashCard.m
//  ManageMoney
//
//  Created by 大明 on 16/4/21.
//  Copyright © 2016年 db. All rights reserved.
//

#import "AddCashCard.h"
#import "ZHPickView.h"
#import "ChooseBankVC.h"
@interface AddCashCard ()<ZHPickViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;/**< 名称*/
@property (weak, nonatomic) IBOutlet UILabel *IDCardLabel;/**< 身份证*/
@property (weak, nonatomic) IBOutlet UITextField *cardNameTextfield;/**< 卡号*/
@property (weak, nonatomic) IBOutlet UITextField *mobileTextfield;/**< 手机号*/
@property (weak, nonatomic) IBOutlet UILabel *cardNameLabel;/**< 卡的名称*/
@property (weak, nonatomic) IBOutlet UITextField *cardBranchTextfield;/**< 卡支行*/
@property(nonatomic,strong)ZHPickView *pickview;
@end

@implementation AddCashCard

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"添加储蓄卡";
    self.nameLabel.text = CheckNull(UserInfo[@"NAME"]);
    self.IDCardLabel.text = CheckNull(UserInfo[@"idCard"]);
//    self.cardNameTextfield.text = @"6227001935240276799";
//    self.mobileTextfield.text = @"18950179627";
    self.cardNameLabel.text = @"中国农业银行";
    [self.cardNameLabel.superview addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cardNameLabelTap)]];
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (_choosebankStr.length>2) {
        self.cardBranchTextfield.text = _choosebankStr;
    }
}

-(void)cardNameLabelTap{
    [_pickview remove];
    NSArray *cardnameArray = [NSArray arrayWithObjects:@"中国农业银行",@"中国工商银行",@"中国建设银行",@"中国银行", nil];
    _pickview=[[ZHPickView alloc] initPickviewWithArray:cardnameArray isHaveNavControler:NO inView:self.view];
//    _pickview.tag = btn.tag;
    _pickview.delegate=self;
    [_pickview show];
}

#pragma mark ZhpickVIewDelegate

-(void)toobarDonBtnHaveClick:(ZHPickView *)pickView resultString:(NSString *)resultString{
    switch (pickView.tag) {
        case 0:
            self.cardNameLabel.text = resultString;
            break;
        case 1:
//            _endDateLab.text = [resultString substringToIndex:10];
            break;
            
        default:
            break;
    }
}
//检索
- (IBAction)retrieveClick:(id)sender {
    
    if ([_cardBranchTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"卡支行不能为空"];
        return;
    }
    
    NSString *retrieveClickUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"cust/queryDepositBankList.do"];
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"bankName":_cardNameLabel.text,@"depositName":_cardBranchTextfield.text};
    
    
    [HWHttpTool post:retrieveClickUrl params:pramas success:^(id json) {
        NSLog(@"%@",json);
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            ChooseBankVC *choosebankvc = [[ChooseBankVC alloc]initWithNibName:@"ChooseBankVC" bundle:nil];
            choosebankvc.ChooseBankArray = dic[@"responseObj"];
            [self.navigationController pushViewController:choosebankvc animated:YES];
            
           
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        [SVProgressHUD dismiss];
    }];
}
//绑定
- (IBAction)bindingClick:(id)sender {
    
    if ([_cardNameTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"卡号不能为空"];
        return;
    }
    
    
    
//    if ([_cardBranchTextfield.text.trim isEqualToString:@""]) {
//        [SVProgressHUD showErrorWithStatus:@"卡支行不能为空"];
//        return;
//    }
    if ([_mobileTextfield.text.trim isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
        return;
    }
    
    NSString *bindingClickUrl = [@"" stringByAppendingFormat:@"%@%@",hrtxUrl,@"fund/bindBankCard"];
    [SVProgressHUD show];
    NSDictionary *pramas  = [[NSDictionary alloc]init];
    pramas = @{@"customerNo":CheckNull(UserInfo[@"NUMBER"]),@"cardNo":_cardNameTextfield.text,@"KHYH":_cardNameLabel.text,@"KHHMC":_cardBranchTextfield.text,@"phone":_mobileTextfield.text};
    
    
    [HWHttpTool post:bindingClickUrl params:pramas success:^(id json) {
        NSLog(@"%@",json);
        [SVProgressHUD dismiss];
        NSDictionary *dic = json;
        if ([dic[@"responseCode"] intValue] == 200) {
            [self leftBarButtonClick:nil];
        }else
        {
            [SVProgressHUD showErrorWithStatus:dic[@"responseMessage"]];
        }
    } failure:^(NSError *error) {
        NSLog(@"错误：%@",error);
        [SVProgressHUD dismiss];
        
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


@end
